package pl.edu.pw.perg.compinfodb.datamodel;

import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import pl.edu.pw.perg.compinfodb.entities.HistoryItem;
import pl.edu.pw.perg.compinfodb.entities.ModuleHistoryItem;
import pl.edu.pw.perg.compinfodb.entities.PhysicalElement;
import pl.edu.pw.perg.compinfodb.entities.VirtualElement;
import pl.edu.pw.perg.compinfodb.manager.IHistoryManager;

public class ModulesHistoryTableModel extends HistoryTableModel {

	private PhysicalElement pe;
	
	public ModulesHistoryTableModel(IHistoryManager historyManager) {
		super(historyManager);
	}
	
	public void selectionChanged(VirtualElement ve) {
		refresh();
		
	}

	public void selectionChanged(PhysicalElement pe) {
		history.clear();
		if(pe != null){
			this.pe = pe;
			history.addAll(historyManager.findPhysicalElementHistory(pe));
		}
		historyChanged(false);
		reloadTable();
	}
	
	@Override
	protected List<HistoryItem> loadItems() {
		return historyManager.findPhysicalElementHistory(pe);
	}
	
	@Override	
	protected HistoryItem createItem() {
		if(pe == null)
			return null;
		ModuleHistoryItem event = new ModuleHistoryItem();
		event.setPhysicalElement(pe);
		return event;
	}
	
	public void ejected(VirtualElement ve, PhysicalElement pe){
		this.pe = pe;
		createEvent("Ejected from " + ve.getName());
		save();
	}
	
	public void inserted(VirtualElement ve, PhysicalElement pe){
		this.pe = pe;
		createEvent("Inserted into " + ve.getName());
		save();
	}
}
